<!DOCTYPE html>
<html lang="en">
<head>
    <style>
        .divcss5 {
            border: 1px solid #F00;
            width: 200px;
            height: 100px
        }
    </style>
    <link rel="stylesheet" href="css/foot.css"/>
    <link rel="stylesheet" href="css/head.css"/>
    <meta http-equiv="Content-type" content="text/html; charset=UTF-8"/>
    <meta name="keywords" content="网上商城, 电商平台, 能源互联网">
    <meta name="description" content="能源互联网, 网上商城, 电商平台, 电子商务, 掌上e宝, 科技金服, 不正经网站研究中心">
    <title>所有商品</title>
    <noscript>抱歉，你的浏览器不支持 JavaScript!</noscript>
    <script type="text/javascript" src="js/base-v1.js" charset="utf-8"></script>
    <script type="text/javascript" src="js/jquery-1.2.6.min.js" charset="utf-8"></script>
    <script type="text/javascript" src="js/cookie.js" charset="utf-8"></script>
    <script type="text/javascript" src="js/common.js" charset="utf-8"></script>
    <script type="text/javascript" src="js/jquery.cookie.js" charset="utf-8"></script>
    <script type="text/javascript" src="js/jquery.min.js" charset="utf-8"></script>
    <script type="text/javascript" src="js/jquery-extend.js"></script>
    <script type="text/javascript" src="js/logout.js"></script>
    <script type="text/javascript" src="js/lib-v1.js" charset="utf-8"></script>
    <link rel="icon" type="image/x-icon" href="img/head/favicon.ico"/>
    <link href="css/prodList.css" rel="stylesheet" type="text/css">
    <meta charset="UTF-8">
    <title>商品分页</title>
    <script type="text/javascript">
        let userId;
        window.onload = function () {
            queryUser();
            let localUrl = decodeURI(window.location.href);
            let queryPageParam = localUrl.substr(localUrl.lastIndexOf("?") + 1);
            if (!/^page=[0-9]*&rows=[0-9]*$/.test(queryPageParam)) {
                queryPages("page=1&rows=30");
                return;
            }
            queryPages(queryPageParam);
        }

        /**
         * 自动登录
         * @param userName 用户名
         * @param userPassword 密码
         */
        function autoLogin(userName, userPassword) {
            $.ajax({
                url:"http://www.supermarket.com/user/autologin",
                type:"get",
                data:{
                    "userName": userName,
                    "userPassword": userPassword
                },
                dataType:"json",
                success: function (data) {
                    if (data.status === 200) {
                        let _data = data.data;//jackson
                        let html = _data.userName +
                            "，欢迎来到Supermarket！<a href='javascript:void(0)' class='link-logout' onclick='logout()'>[退出]</a>";
                        if (_data.userType >= 3)
                            html = html + "&nbsp;&nbsp;|&nbsp;&nbsp;<a href='http://www.Supermarket.com/manage.html' class='link-logout'>[后台]</a>";
                        userId = _data.userId;
                        $("#loginbar").html(html);
                    }else if (data.status === 500){
                        alert(data.msg);
                    }
                },
                error: function () {
                    alert('请求失败');
                }
            });
        }

        function addCartOne(productId, userId) {
            if (productId == null) {
                alert("商品id错误");
                return;
            }
            if (userId === "" || userId == null) {
                window.location.href = "./login.html";
                return;
            }
            $.ajax({
                url: "http://www.supermarket.com/cart/save?productId=" + productId + "&userId=" + userId + "&num=1",
                dataType: "json",
                type: "GET",
                success: function (data) {
                    if (data.status === 200) {
                        window.location.href = "http://www.supermarket.com/mycart.html";
                    } else if (data.status === 201) {
                        alert(data.msg);
                    } else {
                        alert(data.msg);
                    }
                },
                error: function () {
                    alert("请求失败");
                }
            });
        }

        function queryUser() {
            let _ticket = $.cookie("EM_TICKET");
            let userPassword = $.cookie("PASSWORD");
            let userName = $.cookie("USERNAME");
            if (!_ticket) {
                if (!userPassword || !userName)
                    // 二者缺一无法自动登录
                    return;
                else{
                    autoLogin(userName, userPassword);
                    return;
                }
            }
            //当dataType类型为jsonp时，jQuery就会自动在请求链接上增加一个callback的参数
            $.ajax({
                url: "http://www.supermarket.com/user/query/" + _ticket,
                dataType: "json",
                type: "GET",
                success: function (data) {
                    if (data.status === 200) {
                        let _data = JSON.parse(data.data);//jackson
                        let html = _data.userName +
                            "，欢迎来到Supermarket！<a href='javascript:void(0)' class='link-logout' onclick='logout()'>[退出]</a>";
                        if (_data.userType >= 3)
                            html = html + "&nbsp;&nbsp;|&nbsp;&nbsp;<a href='http://www.Supermarket.com/manage.html' class='link-logout'>[后台]</a>";
                        userId = _data.userId;
                        $("#loginbar").html(html);
                    }else if (data.status === 201){
                        // 在redis中没找到用户
                        delCookie("EM_TICKET");
                        delCookie("USERNAME");
                        delCookie("PASSWORD");
                    }else if (data.status === 500){
                        alert(data.msg);
                    }
                },
                error: function () {
                    alert("请求失败");
                }
            });
        }

        function queryPages(param) {
            let ticket = $.cookie("EM_TICKET");
            let userId = "";
            $.get("http://www.supermarket.com/user/query/" + ticket, function (data) {
                if (data.status === 200) {
                    let _data = JSON.parse(data.data);//jackson
                    userId = _data.userId;
                }
            })
            sleep(200);
            $.ajax({
                url: "http://www.supermarket.com/products/pageManage?" + param,
                dataType: "json",
                type: "POST",
                success: function (result) {
                    let data = result.data;
                    if (data.rows.length > 0) {
                        $.each(data.rows, function (index, product) {
                            let product_html = "product-info.html?productId=" + product.productId;
                            let productId = product.productId;
                            $("#prod_content").append(
                                "<div id='prod_div'>" +
                                    "<a href=" + product_html + "><img src='" + product.productImgurl + "' width='200px' height='200px' alt='商品图片'/> </a>" +
                                    "<div id='prod_name_div'><a href=" + product_html + ">" + product.productName + "</a></div>" +
                                    "<div id='prod_price_div'>￥" + product.productPrice + "元</div>" +
                                    "<div>" +
                                        "<div id='gotocart_div'><a href='javascript:void(0)' onclick='addCartOne(\"" + productId + "\",\"" + userId + "\")'>加入购物车</a></div>" +
                                        "<div id='say_div'>库存" + product.productNum + "件</div>" +
                                    "</div>" +
                                "</div>"
                            );
                        })
                    }
                },
                error: function () {
                    alert("请求失败");
                }
            });
        }

        function search(a) {
            let query = document.getElementById(a).value;
            window.location.href = "./search.html?query=" + query;
        }

        function sleep(delay) {
            let start = (new Date()).getTime();
            while ((new Date()).getTime() - start < delay) {
                continue;
            }
        }

        /**
         * 上一页
         */
        function pagePrev() {
            let localUrl = decodeURI(window.location.href);
            let queryPageParam = localUrl.substr(localUrl.lastIndexOf("?") + 1);
            if (!queryPageParam.match("^page=[0-9]*&rows=[0-9]*$")) {
                window.location.href = "./product-list.html?page=1&rows=30";
            } else {
                let page = parseInt(queryPageParam.match(/[0-9]+/g)[0]);
                let rows = parseInt(queryPageParam.match(/[0-9]+/g)[1]);
                if (page === 1)
                    page = 1;
                else
                    page = page - 1;
                window.location.href = "./product-list.html?page=" + page + "&rows=" + rows;
            }
        }

        /**
         * 下一页
         */
        function pageNext() {
            let localUrl = decodeURI(window.location.href);
            let queryPageParam = localUrl.substr(localUrl.lastIndexOf("?") + 1);
            if (!queryPageParam.match("^page=[0-9]*&rows=[0-9]*$")) {
                window.location.href = "./product-list.html?page=1&rows=30";
            } else {
                let page = parseInt(queryPageParam.match(/[0-9]+/g)[0]);
                let rows = parseInt(queryPageParam.match(/[0-9]+/g)[1]);
                page = page + 1;
                window.location.href = "./product-list.html?page=" + page + "&rows=" + rows;
            }
        }

        /**
         * 加载更多
         */
        function showMore() {
            let num = $("#prod_content").children("#prod_div").length;
            let page = Math.floor(num / 30) + 1;
            let localUrl = decodeURI(window.location.href);
            let queryPageParam = localUrl.substr(localUrl.lastIndexOf("?") + 1);
            if (!queryPageParam.match("^page=[0-9]*&rows=[0-9]*$")) {
                queryPages("page=" + page + "&rows=30");
            } else {
                let rows = parseInt(queryPageParam.match(/[0-9]+/g)[1]);
                queryPages("page=" + page + "&rows=" + rows);
            }
        }

    </script>
</head>
<body>
<div id="common_head">
    <div class="line1">
        <div class="content">
            <ul>
                <li class="fore1" id="loginbar" clstag="homepage|keycount|home2013|01b">
                    <span id="head_span">
                        <a href="login.html">登录</a>&nbsp;&nbsp;|&nbsp;&nbsp;
                        <a href="regist.html">注册</a>
                    </span>
                </li>
            </ul>
        </div>
    </div>
    <div class="line2">
        <img id="logo" src="img/head/logo.png" alt="logo"/>
        <input type="text" value="" accesskey="s" id="key" autocomplete="off"
               onkeydown="if(event.keyCode===13) search('key');"/>
        <input type="button" value="搜 索" onclick="search('key')"/>
        <span id="goto">
			<a id="goto_order" href="./myorder.html">我的订单</a>
			<a id="goto_cart" href="./mycart.html">我的购物车</a>
		</span>
        <img id="erwm" src="img/head/e2.png" alt="e2" width="76"/>
    </div>
    <div class="line3">
        <div class="content">
            <ul>
                <li><a href="./index.html">首页</a></li>
                <li><a href="./product-list.html?page=1&rows=30">全部商品</a></li>
                <li><a href="./instant-buy.html">秒杀商品</a></li>
                <li><a href="javascript:void(0)">电脑平板</a></li>
                <li><a href="javascript:void(0)">家用电器</a></li>
                <li><a href="javascript:void(0)">汽车用品</a></li>
                <li><a href="javascript:void(0)">食品饮料</a></li>
                <li><a href="javascript:void(0)">图书杂志</a></li>
                <li><a href="javascript:void(0)">服装服饰</a></li>
                <li><a href="javascript:void(0)">大英理财</a></li>
            </ul>
        </div>
    </div>
</div>

<div id="content" style="display:grid;">
    <div id="prod_content">
    </div>
</div>
<div id="prev_next">
    <a href="javascript:void(0)" onclick="pagePrev()" class="link_button">上一页</a>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
    <a href="javascript:void(0)" onclick="showMore()" class="link_button">加载更多</a>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
    <a href="javascript:void(0)" onclick="pageNext()" class="link_button">下一页</a>
</div>

<div class="fixed">
    <a href="https://github.com/GoogleLLP/SuperMarket" target="_blank">
        <img src="./img/head/qr.jpg" width="100" alt="qrcode"/>
    </a>
    <a href="#common_head" class="toTop"></a>
</div>
<div id="common_foot">
    <p>
        Copyright © 2011-2019 网上商城 版权所有 保留一切权利 粤CM-20201119 | 粤ICP备20201119号-1
        <br />
        粤公网安备  20201119号
    </p>
</div>
</body>
</html>